/*
 * Name: Minh-Tuan ta
 */

package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import actions.*;
import beans.*;
import data.*;

/**
 * Servlet implementation class UserController
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ArrayList<Action> actions;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
        
        actions = new ArrayList<Action>();
    	
    	actions.add(new Home());
    	actions.add(new SetProfilePicture());
    	actions.add(new PageNotFound());
    	actions.add(new Register());
    	actions.add(new Login());
    	actions.add(new Logout());
    	actions.add(new Profile());
    	actions.add(new AddFriend());
    	actions.add(new Friends());
    	actions.add(new DeleteFriend());
    	actions.add(new CreatePost());
    	actions.add(new DeletePost());
    	actions.add(new UpdatePost());
    	actions.add(new CreateComment());
    	actions.add(new DeleteComment());
    	actions.add(new CreateAlbum());
    	actions.add(new DeleteAlbum());
    	actions.add(new UpdateAlbum());
    	actions.add(new UpdateProfile());
    	actions.add(new UploadPhoto());
    	actions.add(new DeletePhoto());
    	actions.add(new Albums());
    	actions.add(new Photos());
    	actions.add(new Rate());
    	actions.add(new DeleteMessage());
    	actions.add(new CreateMessage());
    	actions.add(new Messages());
    	actions.add(new Search());
    	actions.add(new CreateGroup());
    	actions.add(new Groups());
    	actions.add(new GroupUser());
    	actions.add(new ReplyMessage());
    	actions.add(new ViewPhoto());
    }
    
    private Action getAction(String servletPath)
    {
    	Action action = null;
    	
    	for (int i = 0; i < actions.size(); i++)
    	{
    		if (actions.get(i).getURLMapping().equals(servletPath))
    		{
    			action = actions.get(i);
    			break;
    		}
    	}
    	
    	return action;
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//if no url match, move user to the error page
		
		Action action = getAction(request.getServletPath());
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control", "no-cache");
		
		if (action != null)
		{
			action.perform(request, response);
			
			if (request.getMethod().equals("GET"))
			{
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(getLayout());
				dispatcher.forward(request, response);
			}
			else
			{
				response.sendRedirect((String)request.getAttribute("url"));
			}
		}
		else
		{
			response.sendRedirect("PageNotFound");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected String getLayout()
	{
		return "/WEB-INF/views/layout/index.jsp";
	}

}
